Socket (beta version)


Introduction

Note: This software is a beta version and must not be used in game products.

The socket library provides the access to the internet via the Nintendo GameCube broadband adapter. The socket library API has many resemblance to Berkeley sockets which are the de facto standard programming interface for networking. Programs written to Berkeley sockets interface can be ported to GameCube very easily.

The following table illustrates the correspondence of the GameCube socket APIs and Berkeley sockets.

Berkeley socket API GameCube Socket API
accept() SOAccept()
bind() SOBind()
close() SOClose()
connect() SOConnect()
getpeername() SOGetPeerName()
getsockname() SOGetSockName()
getsockopt()
setsockopt()
SOGetSockOpt()
SOSetSockOpt()
htonl()
htons()
ntohl()
ntohs()
SOHtoNl()
SOHtoNs()

SONtoHl()
SONtoHs()
inet_aton() SOInetAtoN()
inet_ntoa() SOInetNtoA()
inet_ntop() SOInetNtoP()
inet_pton() SOInetPtoN()
listen() SOListen()
poll() SOPoll()
recv()
recvfrom()
read()
SORecv()
SORecvFrom()
SORead()
send()
sendto()
write()
SOSend()
SOSendTo()
SOWrite()
shutdown() SOShutdown()
socket() SOSocket()
gethostbyaddr() SOGetHostByAddr()
gethostbyname() SOGetHostByName()
freeaddrinfo() SOFreeAddrInfo()
getaddrinfo() SOGetAddrInfo()
getnameinfo() SOGetNameInfo()

The GCN socket API is primarily different from Berkeley sockets in the following points:

  1. Functions, structures and define names take "SO" prefix. (e.g.,  SOClose(), SOHostEnt, SO_PF_INET)
  2. Functions return an error code rather than -1 if an error occurs for better thread support.
  3. The length of the socket address structure (e.g., SOSockAddrIn) must be set in the len field of the structure. The GCN socket functions do not take the length of the socket address structure via a function argument.

Library layers

Internally, the socket library is implemented in the following four layers:

Socket API
  TCP API     UDP API  
IP API
Network interface API

Low level access methods to the network interface and to the IP routing table have not been well standardized. Therefore, the GameCube socket library lets the program directly access to the following IP APIs to get the current network interface configuration and to modify the IP routing table.

IP API Description
IPGetLinkState Determines whether the network interface is up.
IPGetMacAddr Gets the MAC address of the interface.
IPGetMtu Gets the MTU of the interface.
IPGetAddr Gets the primary internet address of the interface.
IPGetAlias Gets the alias internet address of the interface.
IPGetBroadcastAddr Gets the broadcast address of the interface.
IPGetGateway Gets the default gateway address of the interface.
IPGetNetmask Gets the subnet mask of the interface.
IPClearConfigError Clears the host configuration error code.
IPGetConfigError Gets the host configuration error code.
IPSetConfigError Sets the host configuration error code.
IPInitRoute Initializes the IP routing table.
IPAddRoute Adds the specified data to the routing table.
IPRemoveRoute Removes the specified route from the routing table.
IPRefreshRoute Refreshes the routing table using the base network interface configuration.
IPClearInterfaceStat Clears the interface statistics.
IPGetInterfaceStat Gets the interface statistics.
PPPGetMessage Gets the PAP/CHAP response message.

The socket library currently reserves the following prefixes and an application program should not define functions that begin with theses names:

Prefix Description
ARP address resolution protocol
DHCP dynamic host configuration protocol
DNS domain name system
ICMP internet control message protocol
IF interface
IP internet protocol
SO socket
TCP transmission control protocol
UDP user datagram protocol

Using the socket API

To use the socket library, a program must be linked with ip[D].a and eth[D].a. ip[D].a implements the internet protocol stack. eth[D].a implements the low-level broadband adapter interface.

In a program source code, the following header file must be included:

    #include <dolphin/socket.h>

Configure the network interface

The program must first initialize the socket library with SOInit(). And then the program can configure the network interface with SOStartup().

Note: It is strongly recommended that the program checks the link status of the broadband adapter with IPGetLinkState() before configuring the network interface.

There are three ways to configure the network interface.

  1. Configure the network interface manually.
  2. Configure the network interface via DHCP.
  3. Configure the network interface via PPPoE.

To configure the network interface via DHCP, initialize the SOConfig{} structure as shown below. The SO_FLAG_DHCP flag set in the flag field sets the library to configure the interface using DHCP.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_DHCP,           // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096                    // reassemblyBuffer
};

To configure the network interface via PPPoE, initialize the SOConfig{} structure as shown below. The SO_FLAG_PPPoE flag set in the flag field sets the library to configure the interface using PPPoE. The peerid and passwd fields are used if the access concentrator requires user authentication.

Note: The current implementation supports only PAP and CHAP for user authentication method.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_PPPoE,          // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096,                   // reassemblyBuffer

    0,                      // mtu (default)
    0,                      // rwin (default)
    0,                      // r2 (default)
    
    "user-id",              // peerid
    "password",             // passwd
};

If neither SO_FLAG_DHCP nor SO_FLAG_PPPoE flag is set in the flag field, the interface is configured manually using addr, netmask and router fields.

The host configuration via DHCP or PPPoE can fail from various reasons (DHCP server does not exist, given peerid and passwd is not correct, etc.). If the configuration is failed, a configuration error code is set which can be get with IPGetConfigError(). Once the program calls SOStartup(), it should call IPGetConfigError() repeatedly to verify the network configuration is in normal operation. Once the host configuration is completed, SOGetHostID() returns a non-zero IP address. It can take up to a minute until the host is configured via DHCP or PPPoE.

Dynamically configured host settings can be queried with IPGetAddr() and other IP interface functions.

Note:  Even if the network interface is not configured by any means, it can still broadcast and receive UDP datagrams. 

Note: Without a broadband adapter, sockets bound to the loopback address can be used.

Using domain name system

Up to two DNS server addresses can be specified with SOStartup() or SOSetResolver(). If DNS server addresses are obtained via DHCP or PPPoE, then the assigned server addresses can be retrieved with SOGetResolver(). To look up the host entry, use SOGetHostByAddr() or SOGetHostByName() functions.

Interface and library termination

Once the program no longer needs access to the network, it must shutdown the network interface with SOCleanup(). SOCleanup() releases the internet resources including the IP address assigned from the DHCP server, the PPP session opened with the access concentrator, etc.

A host network connection can be compulsory terminated if the lease time of the IP address assigned from the DHCP server expires, the remote PPPoE access concentrator terminates the current PPP session or the network address collision is detected. The program may check the cause of the error with IPGetConfigError(). If a compulsory termination of the network connection is detected, the program should immediately shutdown the network interface with SOCleanup().

Using transport protocols

Currently the socket library supports TCP and UDP. To use TCP, specify SO_SOCK_STREAM with SOSocket(). To use UDP, specify SO_SOCK_DGRAM with SOSocket().

The programming using transport protocols is basically same as the standard socket network programming. Please refer the popular text books for the socket network programming in general.

Demo programs

dhcp.elf (DHCP host configuration demo )

This program shows how to configure the host with SOStartup() using DHCP.

pppoe.elf (PPPoE host configuration demo )

This program shows hot to configure the host with SOStartup()  using PPPoE.

host.elf (DNS lookup demo)

This program shows how to look up domain name system (DNS) to get the host name from the IP address and vice versa.

httpd.elf (a trivial HTTP server)

This program is a trivial web server implementation. The contents under the 'www' folder in the disc can be accessed with the web browser.

netcfg.elf (Network Setup Screen demo)

This program shows how to load a network configuration file from a memory card and how to call Network Setup Screen.

RFC Compliance

The current socket library implementation supports the following RFCs:

RFC Protocol Description
768 UDP User Datagram Protocol
777 ICMP Internet Control Message Protocol
791 IP Internet Protocol
793 TCP Transmission Control Protocol
826 ARP An Ethernet Address Resolution Protocol
1034, 1035 DNS Domain Name System (stub only)
1332 IPCP The PPP Internet Protocol Control Protocol
1334 PAP PPP Password Authentication Protocol
1661 PPP The Point-to-Point Protocol
1994 CHAP PPP Challenge Handshake Authentication Protocol
2018 TCP Selective Acknowledgment (SACK)
2131, 2132 DHCP Dynamic Host Configuration Protocol
2516 PPPoE PPP Over Ethernet
2581 TCP Slow start, Congestion Avoidance, Fast Retransmit, Fast Recovery, Delayed ACK
3021 IP Using 31-Bit Prefixes on IPv4 Point-to-Point Links

Note: The GameCube socket library employs Berkeley interpretation for the TCP urgent pointer:  The TCP urgent pointer points to the byte next to the urgent byte.

Note: The fast retransmit algorithm is activated only when the SACK option is used between two end points in the GameCube socket implementation. Also the GameCube socket implementation uses the FACK congestion control algorithm instead of the fast recovery algorithm.

RFC 1122 defines the requirements for Internet host software. The current implementation does not implement the following required "MUST" items and thus it is not RFC 1122 compliant. Practically these items are not really important for most network applications of these days as packets with source route options are often simply discarded by routers for security reasons.

Layer Feature Description
IP Source Route Option To be available in the future release
UDP Pass received IP options to application layer To be available in the future release
TCP Source Route Option To be available in the future release

RFC 2525 catalogs a number of known TCP implementation problems. The GameCube socket implementation is adhering to this document for the stability of the global Internet. "SHOULD" items in RFC 1122 that are pointed in RFC 2525 are also implemented in addition to the "MUST" items.